package com.tiancheng.trade.payment.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import jakarta.validation.constraints.NotNull;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * txt文件工具类
 *
 * @Date 2024/11/6
 * @Author DCH
 */
@Slf4j
public final class TxtUtil {

    /**
     * 创建txt文件
     *
     * @param fileName 文件名称
     * @param lines    内容行
     * @param charset  编码
     * @return File
     */
    public static File createTxtFile(@NotNull String fileName,
                                     @NotNull List<String> lines,
                                     @NotNull String charset) {

        File txtFile = new File(fileName);
        if (CollectionUtils.isEmpty(lines)) {
            return txtFile;
        }
        charset = Optional.ofNullable(charset).filter(c -> !StringUtils.isEmpty(c)).orElse("utf-8");
        // 缓冲方式写入文件更为高效
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtFile), charset))) {
            for (int i = 0; i < lines.size(); i++) {
                bw.write(lines.get(i));
                if (i < (lines.size() - 1)) {
                    bw.newLine();
                }
            }
        } catch (IOException e) {
            log.error("createTxtFile error: " + e);
        }
        return txtFile;
    }

    /**
     * 读取CSV文件
     *
     * @param filePath    txt文件的绝对文件路径
     * @param charsetName 编码
     * @return
     * @throws IOException
     */
    public static Object[][] getData(String filePath, String charsetName) {
        List<String[]> records = new ArrayList<String[]>();
        String record;
        try {
            BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), charsetName));
            while ((record = file.readLine()) != null) {
                String fields[] = record.split("\\|");
                records.add(fields);
            }
            // 关闭文件
            file.close();
        } catch (Exception ex) {
            log.error("TxtUtil getData 异常" + ex.getMessage());
        }

        // 将存储测试数据的List转换为一个Object的二维数组
        Object[][] results = new Object[records.size()][];
        // 设置二位数组每行的值，每行是一个Object对象
        for (int i = 0; i < records.size(); i++) {
            results[i] = (Object[]) records.get(i);
        }
        return results;
    }

    /**
     * 读取V文件
     *
     * @param is          文件流
     * @param charsetName 编码
     * @return
     * @throws IOException
     */
    public static Object[][] getDataFromInputStream(InputStream is, String charsetName) {
        List<String[]> records = new ArrayList<String[]>();
        String record;
        try {
            BufferedReader file = new BufferedReader(new InputStreamReader(is, charsetName));
            while ((record = file.readLine()) != null) {
                String fields[] = record.split("\\|");
                records.add(fields);
            }
            // 关闭文件
            file.close();
        } catch (Exception ex) {
            log.error("TxtUtil getData 异常" + ex.getMessage());
        }

        // 将存储测试数据的List转换为一个Object的二维数组
        Object[][] results = new Object[records.size()][];
        // 设置二位数组每行的值，每行是一个Object对象
        for (int i = 0; i < records.size(); i++) {
            results[i] = (Object[]) records.get(i);
        }
        return results;
    }
    /*public static void main(String[] args) {
        List<String> lines = Arrays.asList("001|002|003", "003|002|001");
        File file = createTxtFile("D:/sftp/test001.txt", lines, "utf-8");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/
}
